package com.microservice.postservice.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.microservice.commonutils.R;
import com.microservice.commonutils.userVo.UserInfoVo;
import com.microservice.postservice.client.UserInfoClient;
import com.microservice.postservice.entity.PostContent;
import com.microservice.postservice.entity.PostImage;
import com.microservice.postservice.entity.vo.PostCommentVo;
import com.microservice.postservice.entity.vo.PostContentImageVo;
import com.microservice.postservice.service.PostContentService;
import com.microservice.postservice.service.PostImageService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author leesin
 * @since 2022-05-14
 */
@RestController
@RequestMapping("/postservice/postcontent")
public class PostContentController {

    @Autowired
    private PostContentService postContentService;

    @Autowired
    private PostImageService postImageService;

    @Autowired
    private UserInfoClient userInfoClient;

    @GetMapping("findAll")
    public R findAll(){
        List<PostContentImageVo> postContentAndImageAll = postContentService.getPostContentAndImageAll();
        return R.ok().data("post",postContentAndImageAll);
    }

    @GetMapping("pagePost/{current}/{limit}")
    public R pageListPost(@PathVariable long current, @PathVariable long limit){
        //创建page对象
        Page<PostContent> postContent =new Page<>(current,limit);
        QueryWrapper wrapper=new QueryWrapper();
        wrapper.orderByDesc("gmt_create");
        postContentService.page(postContent, wrapper);
        long total =postContent.getTotal();
        List<PostContent> record=postContent.getRecords();

        List<PostContentImageVo> postContentImageVoList=new ArrayList<>();
        for (int i = 0; i < record.size(); i++) {
            UserInfoVo userInfoById = userInfoClient.getUserInfoById(record.get(i).getUserId());
            List<PostImage> imageList = postImageService.getImageList(record.get(i).getId());

            PostContentImageVo postContentImageVo=new PostContentImageVo();
            postContentImageVo.setUserInfoVo(userInfoById);
            postContentImageVo.setPostImageList(imageList);
//            postContentImageVo.setContent(record.get(i).getContent());
//            postContentImageVo.setLikes(record.get(i).getLikes());
            BeanUtils.copyProperties(record.get(i),postContentImageVo);
            postContentImageVoList.add(postContentImageVo);
        }

        return R.ok().data("total",total).data("rows",postContentImageVoList);
    }


//
    @GetMapping("getPostById/{id}")
    public R getPostById(@PathVariable(value = "id")String id){
        List<PostContentImageVo> postContentAndImage = postContentService.getPostContentAndImage(id);
        return R.ok().data("post",postContentAndImage);

    }


    @GetMapping("getContentAndImage")
    public R getContentAndImage(String postId){
        List<PostContentImageVo> postContentAndImage = postContentService.getPostContentAndImage(postId);
        return R.ok().data("post",postContentAndImage);
    }


    /**
     * 添加贴子
     * @param postContent
     * @return
     */
    @PostMapping("insertpost")
    public R insertpost(@RequestBody PostContent postContent){
        boolean save = postContentService.save(postContent);
        if (save){
            return R.ok().data("postContent",postContent);
        }else {
            return R.error();
        }

    }
}

